/* 
 * shamefully copy from AnFetion.
 * more see: http://code.google.com/p/anfetion/
 */
package person.bangbang.im.Androidgin.Fetion;

import java.util.ArrayList;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

// 获得自己的信息.
public class GetPersonalInfo extends SIPCBase {
    private FetionUri fetionid;
    private FetionFriend info;

    private FetionSIPC sipc;

    public GetPersonalInfo(SerialManager serialManager, TcpSendAndReceive connection, FetionUri fetionid,
            FetionFriend info) {
        super(serialManager, connection);
        this.fetionid = fetionid;
        this.info = info;
    }

    @Override
    public boolean execute() {
        ArrayList<FetionSIPC.HeaderPair> headers = new ArrayList<FetionSIPC.HeaderPair>();
        headers.add(new FetionSIPC.HeaderPair("F", this.fetionid.getId()));
        headers.add(new FetionSIPC.HeaderPair("I", serialManager.getISerial() + " "));
        headers.add(new FetionSIPC.HeaderPair("Q", serialManager.getQSerial() + " S"));
        headers.add(new FetionSIPC.HeaderPair("N", "GetPersonalInfo"));

        StringBuilder body = new StringBuilder();
        body.append("<args><personal attributes=\"all\" />"
                + "<services version=\"\" attributes=\"all\" /><quota attributes=\"all\" /></args>");
        headers.add(new FetionSIPC.HeaderPair("L", StrUtil.getLength(body)));

        sipc = new FetionSIPC(SIPC_COMMON_HEADER, headers, body.toString());
        if (this.SendAndReceiveResponse(sipc)) {
            try {
                if (sipc.response.getResponseCmdLine().code.equals(SIPC_RESPONSE_CODE_OK)) {
                    Document dom = parseXmlDocument(sipc.response.body);
                    Element results = dom.getDocumentElement();

                    if (results != null && results.getNodeName().equals("results")) {
                        Node node = results.getFirstChild();
                        if (node != null && node.getNodeName().equals("personal")) {
                            NamedNodeMap attr = node.getAttributes();
                            Node n = attr.getNamedItem("nickname");
                            if (n != null) info.setNickName(n.getNodeValue());
                            n = attr.getNamedItem("impresa");
                            if (n != null) info.impresa = n.getNodeValue();
                            n = attr.getNamedItem("mobile-no");
                            if (n != null && !n.getNodeValue().equals("")) info.mobile_no = n.getNodeValue();
                            n = attr.getNamedItem("portrait-crc");
                            if (n != null && !n.getNodeValue().equals("")) info.portrait = StrUtil.parseLong(
                                    n.getNodeValue(), 0);
                            return true;
                        }
                    }
                }
            } catch (NumberFormatException e) {
                this.onSetText(e.getMessage());
                e.printStackTrace();
            } catch (DOMException e) {
                this.onSetText(e.getMessage());
                e.printStackTrace();
            } catch (Exception e) {
                this.onSetText(e.getMessage());
                e.printStackTrace();
            }
        }
        return false;
    }
}
